package com.example.plumneedle.Framgment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.plumneedle.Adapter.LeftListAdapter;
import com.example.plumneedle.Adapter.MainSectionedAdapter;
import com.example.plumneedle.R;
import com.example.plumneedle.method.PinnedHeaderListView;

import butterknife.BindView;
import butterknife.ButterKnife;


public class BlankFragment2 extends Fragment {

    private PinnedHeaderListView pinnedListView;
    private ListView leftListview;
    private MainSectionedAdapter sectionedAdapter;
    private boolean isScroll = true;
    private LeftListAdapter adapter;

    private String[] leftStr = new String[]{"解毒药", "清热药", "理气药", "活血化瘀药"};
    private boolean[] flagArray = {true, false, false, false};
    private String[][] rightStr = new String[][]{{"夏桑菊", "夏枯草", "桑叶","野菊花","连翘"},
            {"谷精草", "蜜蒙花", "秦皮","白藓皮","紫草","马齿苋","白头翁","鸦胆子","地骨皮","胡黄连"},
            {"橘皮", "乌药", "沉香","甘松","刀豆"}, {"三七", "乳香", "川芎"}
    };
    private int[][] yao= new int[][]{{R.drawable.yao1,R.drawable.ku,R.drawable.sang,R.drawable.ju,R.drawable.lian},{R.drawable.gu1,R.drawable.mi,R.drawable.qing,R.drawable.bai,R.drawable.chai,
            R.drawable.ma,R.drawable.to,R.drawable.ya,R.drawable.di,R.drawable.hu},{R.drawable.pi,R.drawable.wi,R.drawable.chen,R.drawable.guan,R.drawable.dao},{R.drawable.san,R.drawable.ru
            ,R.drawable.chuan}};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank2, container, false);
    }
    public void onActivityCreated(@Nullable Bundle saveInstanceState) {
        super.onActivityCreated(saveInstanceState);
        ButterKnife.bind(getActivity());
        pinnedListView = (PinnedHeaderListView) getActivity().findViewById(R.id.pinnedListView);
        leftListview=getActivity().findViewById(R.id.left_listview);
        sectionedAdapter = new MainSectionedAdapter(getActivity(), leftStr, rightStr,yao);
        pinnedListView.setAdapter(sectionedAdapter);
        adapter = new LeftListAdapter(getActivity(), leftStr, flagArray);
        leftListview.setAdapter(adapter);
        leftListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
                isScroll = false;

                for (int i = 0; i < leftStr.length; i++) {
                    if (i == position) {
                        flagArray[i] = true;
                    } else {
                        flagArray[i] = false;
                    }
                }
                adapter.notifyDataSetChanged();
                int rightSection = 0;
                for (int i = 0; i < position; i++) {
                    rightSection += sectionedAdapter.getCountForSection(i) + 1;
                }
                pinnedListView.setSelection(rightSection);

            }

        });

        pinnedListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView arg0, int scrollState) {
                switch (scrollState) {
                    // 当不滚动时
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        // 判断滚动到底部
                        if (pinnedListView.getLastVisiblePosition() == (pinnedListView.getCount() - 1)) {
                            leftListview.setSelection(ListView.FOCUS_DOWN);
                        }

                        // 判断滚动到顶部
                        if (pinnedListView.getFirstVisiblePosition() == 0) {
                            leftListview.setSelection(0);
                        }

                        break;
                }
            }

            int y = 0;
            int x = 0;
            int z = 0;

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (isScroll) {
                    for (int i = 0; i < rightStr.length; i++) {
                        if (i == sectionedAdapter.getSectionForPosition(pinnedListView.getFirstVisiblePosition())) {
                            flagArray[i] = true;
                            x = i;
                        } else {
                            flagArray[i] = false;
                        }
                    }
                    if (x != y) {
                        adapter.notifyDataSetChanged();
                        y = x;
                        if (y == leftListview.getLastVisiblePosition()) {
//                            z = z + 3;
                            leftListview.setSelection(z);
                        }
                        if (x == leftListview.getFirstVisiblePosition()) {
//                            z = z - 1;
                            leftListview.setSelection(z);
                        }
                        if (firstVisibleItem + visibleItemCount == totalItemCount - 1) {
                            leftListview.setSelection(ListView.FOCUS_DOWN);
                        }
                    }
                } else {
                    isScroll = true;
                }
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.bind(getActivity());
    }
}
